<template>
    <div class='myFocus'>
        <div class='msg-container'>
            <div class='header-info'>
                <el-form :model='queryParams' @keydown.prevent.enter='queryHandler'>
                    <el-input
                        v-model='queryParams.params.nickName' clearable placeholder='搜索作者'
                        @clear='queryHandler' />
                </el-form>
            </div>
            <ul class='focus-list' v-if='focusList.length'>
                <li v-for='focusItem in focusList' :key='focusItem.focusId' class='focus-item'>
                    <div class='left'>
                        <img v-if='focusItem?.receiveUser?.avatar' :src='`/api${focusItem?.receiveUser?.avatar}`'
                             alt='avatar'>
                        <img v-else src='@/assets/images/profile.jpg' alt='avatar'>
                        <div>{{ focusItem?.receiveUser?.nickName || '用户名' }}</div>
                    </div>
                    <el-button
                        v-if='focusItem.status' plain type='danger' size='small'
                        @click='focusHandler(focusItem.receiveId)'>取消关注
                    </el-button>
                    <el-button v-else plain type='success' size='small' @click='focusHandler(focusItem.receiveId)'>
                        关注
                    </el-button>
                </li>
            </ul>
            <div v-else class='empty'>
                暂无关注数据
            </div>
            <div class='pagination'>
                <pagination
                    v-show='total > 0'
                    v-model:limit='queryParams.pageSize'
                    v-model:page='queryParams.pageNum'
                    :total='total'
                    layout='total, prev, pager, next'
                    @pagination='getList'
                />
            </div>
        </div>

    </div>
</template>
<script setup lang='ts'>
import { getCurrentInstance, reactive, ref, toRefs } from 'vue';
import { focus, getMyFocus } from '@/api/business/focus';
import { Focus } from '@/api/model/Focus';

const { proxy } = getCurrentInstance();
const total = ref(0);
const focusList = ref<Focus[]>([]);
/*请求参数定义*/
const data = reactive({
    form: {},
    queryParams: { pageNum: 1, pageSize: 24, params: {} }
});
const { form, queryParams } = toRefs(data);
const getList = () => {
    getMyFocus(queryParams.value).then(res => {
        res.rows.forEach((item: Focus) => {
            item['status'] = true;
        });
        focusList.value = res.rows;
        total.value = res.total;
    });
};
/*取消关注*/
const focusHandler = (receiveId: number | string) => {
    focus(receiveId).then(() => {
        const find = focusList.value.find(x => x.receiveId === receiveId);
        if (find) {
            find['status'] = !find['status'];
        }
        proxy.$modal.msgSuccess('操作成功!');
    });
};
/*查询*/
const queryHandler = () => {
    queryParams.value.pageNum = 1;
    getList();
};
getList();
</script>
<style scoped lang='scss'>
.msg-container {
    border: 1px solid #efefef;
    padding: 20px;
    min-height: 500px;

    .header-info {
        display: flex;
        align-items: center;
        justify-content: space-between;

        :deep(.el-input__wrapper) {
            box-shadow: none;
            background: #F2F6FC;
        }
    }

    .focus-list {
        margin-top: 20px;
        display: flex;
        flex-wrap: wrap;

        .focus-item {
            margin-right: 10px;
            cursor: pointer;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            height: 50px;
            background: #F2F6FC;
            border-radius: 4px 4px 4px 4px;
            justify-content: space-between;
            padding: 10px;
            width: calc(25% - 7.5px);

            &:hover {
                button {
                    opacity: 1;
                }
            }

            button {
                opacity: 0;
                transition: all 0.3s;
            }

            &:nth-child(4n) {
                margin-right: 0;
            }

            .left {
                display: flex;
                align-items: center;
            }

            img {
                width: 40px;
                height: 40px;
                border-radius: 40px;
                margin-right: 10px;
            }
        }
    }
}
</style>
